package procsim;

import java.awt.*;

public class GDFlipFlop extends DFlipFlop implements GraphicalElement {

    private int width = 60;
    private int height = 80;
    private int x, y;

    public GDFlipFlop() {
        super();
    }

    public GDFlipFlop(Signal in) {
        super(in);
    }

    @Override
    public void setCoords(int x, int y){
        this.x = x;
        this.y = y;
    }

    @Override
    public int getWidth() {
        return width;
    }

    @Override
    public int getHeight() {
        return height;
    }

    @Override
    public void setSize(int width, int height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public void drawIt(Graphics2D g, int x, int y) {
        Util.saveGraphics(g);
        g.setColor(Color.BLACK);
        g.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
        g.drawRect(x+1, y+1, width-2, height-2);
        g.drawString("D", x+5, y+20);
        g.drawString("clk", x+5, y+height/2+5);
        g.drawString("Q", x+width-12, y+20);
        g.drawLine(x+width-12, y+height-23, x+width-6, y+height-23);
        g.drawString("Q", x+width-12, y+height-12);
        g.drawString("E", x+width/2-3, y+14);
        g.drawString("Rd", x+width/2-6, y+height-5);
        g.setFont(new Font(Font.MONOSPACED, Font.BOLD, 14));
        g.drawOval(x+getWidth()/2-2, y+getHeight(), 4, 4);
        Coords position = this.getRdCoords(11);
        g.setFont(new Font("Arial", Font.PLAIN, 10));
        g.drawLine(position.getX1()+3, position.getY1()+2, position.getX1()+16, position.getY1()+2);
        g.drawString("mr", position.getX1()+3, position.getY1()+10);
        g.drawLine(position.getX1(), position.getY1(), position.getX2(), position.getY2());
        Util.restoreGraphics(g);
    }

    @Override
    public void drawIt(Graphics2D g) {
        drawIt(g, x, y);
    }

    public Coords getDCoords(int length){
        return new Coords(x-length, y+17, x, y+17);
    }

    public Coords getQCoords(int length){
        return new Coords(x+width, y+17, x+width+length, y+17);
    }

    public Coords getQComplCoords(int length){
        return new Coords(x+width, y+height-17, x+width+length, y+height-17);
    }

    private Coords getRdCoords(int length){
        return new Coords(x+width/2, y+height+5, x+width/2, y+height+5+length);
    }
}
